home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_097 / splines / bezier.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  65 lines

  1. /* The routines in this file are copyright (c) 1987 by Helene (Lee) Taran.
  2.  * Permission is granted for use and free distribution as long as the
  3.  * original author's name is included with the code.
  4.  */
  5.  
  6. #include "bezier.h"
  7.  
  8. /* Midpoint: returns the midpoint of the line through <p0> and <p1> */
  9. Midpoint (m, p0 , p1)
  10. REAL_POINT *m, *p0, *p1;
  11. {  
  12.    m->x = (p0->x + p1->x)/2 ;
  13.    m->y = (p0->y + p1->y)/2 ;
  14. }
  15.  
  16.  
  17. /* TickMark : returns the point that is n/d of the way 
  18.  * between <p0> and <p1>. If you just want to divide the line in
  19.  * half use Midpoint.
  20.  */
  21. TickMark (t, n, d, p0, p1)
  22. REAL_POINT *t;
  23. int n, d; REAL_POINT *p0, *p1;
  24. {  
  25.    t->x = p0->x +  (p1->x - p0->x) * n/d ;
  26.    t->y = p0->y +  (p1->y - p0->y) * n/d ;
  27. }
  28.  
  29.  
  30. /* DrawBezierArc: draws a bezier arc between the 4 given points by
  31.  * recursively subdividing the curve until each segment is close
  32.  * enough to be rendered as a line.
  33.  * Assumes that your window <w> has been initialized and opened and that
  34.  * in addition to the intuition library, the graphics library is open
  35.  */
  36. DrawBezierArc(w, p000, p001, p011, p111)
  37. struct Window *w;                          /* draw the arc in this window */
  38. REAL_POINT *p000, *p001, *p011, *p111;   /* bezier control points */
  39.    REAL_POINT p00t,p0t1,pt11,p0tt,ptt1,pttt;
  40.    float delta1 = (p001->x * p011->y - p001->y * p011->x) -
  41.                   (p000->x * p011->y - p000->y * p011->x) +
  42.                    (p000->x * p001->y - p000->y * p001->x);
  43.  
  44.    float delta2 = (p111->x * p011->y - p111->y * p011->x) -
  45.                   (p000->x * p011->y - p000->y * p011->x) +
  46.           (p000->x * p111->y - p000->y * p111->x);
  47.  
  48.    if (ABS(delta1) + ABS(delta2) <= CLOSENESS) { /* so close, just draw a line */
  49.       Move(w->RPort,(long)p000->x,(long)p000->y);
  50.       Draw(w->RPort,(long)p111->x,(long)p111->y);
  51.      }
  52.    else { /* divide the arc into two smaller bezier arcs */
  53.        Midpoint(&p00t, p000, p001);
  54.        Midpoint(&p0t1, p001, p011);
  55.        Midpoint(&pt11, p011, p111);
  56.        Midpoint(&p0tt, &p00t, &p0t1);
  57.        Midpoint(&ptt1, &p0t1, &pt11);      
  58.        Midpoint(&pttt, &p0tt, &ptt1);
  59.        DrawBezierArc(w,p000,&p00t,&p0tt,&pttt);
  60.        DrawBezierArc(w,&pttt,&ptt1,&pt11,p111);
  61.    }
  62. }   
  63.       
  64.